//
//  QDQuestTextTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/3.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 问题描述 简述问题
final class QDQuesTextTableViewCell: NWTableViewCell,QDQuesFeedTableViewCell {

    /// 显示标题
    let titleLabel = UILabel()
    /// 文本框
    let textView = RSKPlaceholderTextView()
    /// 开始的回调
    var beginHandler: ((UITextView) -> ())?
    /// 结束的回调
    var endHandler: ((UITextView) -> ())?
    
    /// 限制个数的标签
    private let countLabel = UILabel()

    override func buildView() {
        super.buildView()
        titleLabel.text = "请简单描述您的问题"
        initTitleLabel()
        makeConstraintsForTitleLabel()
        
        textView.layer.borderColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        textView.layer.borderWidth = 1
        textView.delegate = self
        textView.layer.cornerRadius = 8
        textView.tintColor = QDColors.theme
        textView.textColor = QDColors.black
        textView.font = Font.regular.font(size: 16)
        textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
        textView.placeholderColor = #colorLiteral(red: 0.7607843137, green: 0.768627451, blue: 0.7647058824, alpha: 1)
        textView.placeholder = "在这里描述你的问题吧~"
        
        countLabel.textColor = QDColors.gray
        countLabel.font = Font.regular.font(size: 12)
        countLabel.text = "0/100"
        
        contentView.addSubview(textView)
        contentView.addSubview(countLabel)
        
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().inset(32)
        }
        
        countLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(41)
            make.bottom.equalTo(textView.snp.bottom).inset(10)
            make.height.equalTo(17)
        }
    }
}

extension QDQuesTextTableViewCell: UITextViewDelegate {
    
    /// 变化，变为计数器
    func textViewDidChange(_ textView: UITextView){
        
        let text = textView.text as NSString
        
        countLabel.text = "\(text.length)/100"
        countLabel.textColor = text.length > 100 ? .red : QDColors.black
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        beginHandler?(textView)
    }
    
    
    func textViewDidEndEditing(_ textView: UITextView) {
        endHandler?(textView)
    }
}
